package main

import (
	"fmt"
	"time"
	"tushare/model/mysql"
	"tushare/service/aliyunCli"
	"tushare/service/appCommand"
	serviceSsh "tushare/service/ssh"

	"github.com/spf13/cobra"
)

func main() {
	mysql.InitDB()
	defer mysql.DB.Close()

	var cmdDescribeRegions = &cobra.Command{
		Use:   "describeRegions",
		Short: "查询可以使用的阿里云地域",
		Long:  `查询可以使用的阿里云地域`,
		Run: func(cmd *cobra.Command, args []string) {
			describeRegions := new(aliyunCli.DescribeRegions)
			describeRegions.DescribeRegions()
		},
	}

	var cmdCreateInstance = &cobra.Command{
		Use:   "createInstance",
		Short: "创建一台包年包月或者按量付费ECS实例",
		Long:  `创建一台包年包月或者按量付费ECS实例`,
		Run: func(cmd *cobra.Command, args []string) {
			for i := 0; i < 1; i++ {
				keyPairName := "hadoop_two"
				fmt.Println("创建阿里云ecs.c6a.xlarge")
				ci := new(aliyunCli.CreateInstance)
				id := ci.CreateInstance("", "", "")
				time.Sleep(time.Second * 30)
				fmt.Println("阿里云ecs.c6a.xlarge创建成功")

				fmt.Println("阿里云ecs.c6a.xlarge创建公网IP")
				ai := new(aliyunCli.AllocatePublicIpAddress)
				ai.AllocatePublicIpAddress(id)
				time.Sleep(time.Second * 30)
				fmt.Println("阿里云ecs.c6a.xlarge创建公网IP创建成功")

				fmt.Println("阿里云ecs.c6a.xlarge绑定登录秘钥")
				akp := new(aliyunCli.AttachKeyPair)
				akp.AttachKeyPair(id, keyPairName)
				time.Sleep(time.Second * 30)
				fmt.Println("阿里云ecs.c6a.xlarge登录秘钥绑定成功")

				fmt.Println("阿里云ecs.c6a.xlarge启动")
				si := new(aliyunCli.StartInstance)
				si.StartInstance(id)
				time.Sleep(time.Second * 30)
				fmt.Println("阿里云ecs.c6a.xlarge启动成功")
				di := new(aliyunCli.DescribeInstances)
				ip := di.DescribeInstances(id)

				fmt.Println(id)
				fmt.Println(ip)
			}

		},
	}

	var cmdDescribeInstances = &cobra.Command{
		Use:   "describeInstances",
		Short: "查询实例详情",
		Long:  `查询实例详情`,
		Run: func(cmd *cobra.Command, args []string) {
			di := new(aliyunCli.DescribeInstances)
			di.DescribeInstances("")
		},
	}

	var cmdAllocatePublicIpAddress = &cobra.Command{
		Use:   "allocatePublicIpAddress",
		Short: "分配公网ip",
		Long:  `分配公网ip`,
		Run: func(cmd *cobra.Command, args []string) {
			ai := new(aliyunCli.AllocatePublicIpAddress)
			ai.AllocatePublicIpAddress("")
		},
	}

	var cmdCreateKeyPair = &cobra.Command{
		Use:   "createKeyPair",
		Short: "创建登录秘钥",
		Long:  `创建登录秘钥`,
		Run: func(cmd *cobra.Command, args []string) {
			keyNmae := args[0]
			ckp := new(aliyunCli.CreateKeyPair)
			ckp.CreateKeyPair(keyNmae)
		},
	}

	var cmdDeleteInstance = &cobra.Command{
		Use:   "deleteInstance",
		Short: "删除实例",
		Long:  `删除实例`,
		Run: func(cmd *cobra.Command, args []string) {
			instanceId := args[0]
			di := new(aliyunCli.DeleteInstance)
			di.DeleteInstance(instanceId)
		},
	}

	var cmdInstallApp = &cobra.Command{
		Use:   "installApp",
		Short: "安装App",
		Long:  `安装App`,
		Run: func(cmd *cobra.Command, args []string) {
			appType := args[0]
			ip := args[1]
			ssh := new(serviceSsh.Ssh)
			ssh.InstallApp(appType, ip)
		},
	}

	var cmdAppCommand = &cobra.Command{
		Use:   "appCommand",
		Short: "app管理命令",
		Long:  `app管理命令`,
		Run: func(cmd *cobra.Command, args []string) {
			appName := args[0]
			command := args[1]
			ams := new(appCommand.AppManageShell)
			ams.ManageApp(appName, command)
		},
	}

	var rootCmd = &cobra.Command{Use: "app"}
	rootCmd.AddCommand(cmdDescribeRegions)
	rootCmd.AddCommand(cmdCreateInstance)
	rootCmd.AddCommand(cmdDescribeInstances)
	rootCmd.AddCommand(cmdAllocatePublicIpAddress)
	rootCmd.AddCommand(cmdCreateKeyPair)
	rootCmd.AddCommand(cmdDeleteInstance)
	rootCmd.AddCommand(cmdInstallApp)
	rootCmd.AddCommand(cmdAppCommand)
	rootCmd.Execute()
}
